/**
 * Created by TRwin7SP1 on 2016/8/15.
 */
angular.module('inspinia')
  .controller('loginCtrl', function Login($scope, $http, $rootScope, $cookieStore,$state,users,toaster) {

/*
    $scope.currState = $state;
    console.log($state.current.data);
    $scope.$watch('currState.current.name', function(newValue, oldValue) {
      console.log(oldValue);
    });
*/

    $scope.username = '';
    $scope.password = '';

    $scope.login = function(){

      window.location.href = "/#/index/main";
      localStorage.setItem('user',JSON.stringify(users[0]));


      // var data = {
      //   username:$scope.username,
      //   password:$scope.password
      // }

      // var url = "http://192.168.100.106:8000/api/v1/user/login";
      // var fun_login_success = function (data,status,headers,config) {
      //   console.log("data:"+JSON.stringify(data,null,4));
      //   console.log("status:"+status);
      //   console.log("headers:"+JSON.stringify(headers,null,4));
      //   console.log("config:"+JSON.stringify(config,null,4));
      //
      //   window.location.href = "/#/index/main";
      // };
      //
      //
      //
      // var fun_login_err = function (data,status,headers,config) {
      //   console.log("data:"+JSON.stringify(data,null,4));
      //   console.log("status:"+status);
      //   console.log("headers:"+JSON.stringify(headers,null,4));
      //   console.log("config:"+JSON.stringify(config,null,4));
      //   window.location.href = "/#/index/main";
      // };

      // $http.get(url).success(fun_login_success).error(fun_login_err);
      // $http.post(url,data).success(fun_login_success).error(fun_login_err);
      //
      //
      // $http({
      //   method: "post",
      //   data:{
      //     username:$scope.username,
      //     password:$scope.password
      //   },
      //   url:url,
      //   headers: { "Content-Type": "application/x-www-form-urlencoded" },
      // }).success(fun_login_success).error(fun_login_err);




  /*    for(var i=0;i<users.length;i++){
        if(users[i].username === $scope.username && users[i].password === $scope.password){
          toaster.pop({
            type: 'success',
            title: '登陆成功',
            body: '欢迎来到连心医疗',
            showCloseButton: true,
            timeout: 2000
          });
          localStorage.setItem('user',JSON.stringify(users[i]));
          window.location.href = "/#/index/main";
          return;
        }
      }*/
    };
    /*var localStorage = window.localStorage;
    if ($cookieStore.get("rmbUser") == "true") {
      $scope.remember = true;
      $scope.username = $cookieStore.get('username');
      $scope.password = $cookieStore.get('password');
    }
    $scope.login = function (username, password, type) {
      $http.post($rootScope.servers + "/service/user/login", {
        'username': $scope.username,
        'password': $scope.password
      })
        .success(function (data) {

          var result = data.result;
          if (result == 0) {
            var user = JSON.parse(data.message)[0];
            localStorage.setItem("userid", user.userid);
            localStorage.setItem("password", password);
            localStorage.setItem("usertype", user.usertype);
            localStorage.setItem("user", JSON.stringify(user));
            if (user.status == 5) {
              myToaster("error", "登录失败", "您已被拉黑，请联系管理员。");
            }
            else if(user.status==1||user.status==3){
              myToaster("success","登陆成功","欢迎来到三健医疗");
              if(user.usertype==3){
                window.location.href = "/#/instituteUpdateNopass";
              }else if(user.usertype==2){
                $state.go("navigationDoctorNopass");
              }

            }
            else {
              toaster.pop({
                type: 'success',
                title: '登陆成功',
                body: '欢迎来到三健医疗',
                showCloseButton: true,
                timeout: 2000
              });
              if (data.message) {
                if ($scope.remember == true) {
                  $cookieStore.put('username', $scope.username, {expires: 7});
                  $cookieStore.put("rmbUser", 'true', {expires: 7}); // 存储一个带7天期限的 cookie
                  $cookieStore.put("password", $scope.password, {expires: 7}); // 存储一个带7天期限的 cookie
                }
                else {
                  $cookieStore.remove('username');
                  $cookieStore.remove('password');
                  $cookieStore.remove('rmbUser');
                }
                var usertype = user.usertype;
                if (usertype == 1) {
                  window.location.href = "/#/homePage";
                } else if (usertype == 2) {
                  window.location.href = "/#/doctor_main";
                } else if (usertype == 3) {
                  window.location.href = "/#/homePage";
                } else if (usertype == 0) {
                  window.location.href = "/#/adminPage"
                }
              }
            }
          }
          else if (result == 1) {
            toaster.pop({
              type: 'warning',
              title: '登录失败',
              body: '用户名不存在',
              showCloseButton: true,
              timeout: 1000
            });
          }
          else if (result == 2) {
            toaster.pop({
              type: 'error',
              title: '登录失败',
              body: '密码错误',
              showCloseButton: true,
              timeout: 1000
            });
          }
          else if (result == undefined) {
            toaster.pop({
              type: 'warning',
              title: '登录失败',
              body: '该浏览器不支持本系统，请更新浏览器',
              showCloseButton: true,
              timeout: 1000
            });
          }
          else {
            toaster.pop({
              type: 'error',
              title: '登录失败',
              body: '服务器异常',
              showCloseButton: true,
              timeout: 1000
            });
          }
        }).error(function (data) {
        toaster.pop({
          type: 'error',
          title: '登录失败',
          body: '服务器异常',
          showCloseButton: true,
          timeout: 1000
        });
      })
    };*/
});
